﻿
Imports Moca.Db
Imports Moca.Win

Public Class Generate

    Private _dt As DataTable

	Private _dbc As DbContextProperty

    Public Property Values() As System.Data.DataTable
        Get
            Return Me._dt
        End Get
        Set(ByVal value As System.Data.DataTable)
            Me._dt = value
        End Set
    End Property

    Private Sub Generate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.AllowUserToAddRows = False

        Me.PropertyGrid1.SelectedObject = My.Settings

        Dim dt As DataTable

        dt = New DataTable("")
        dt.Columns.Add("ColumnName")
        'dt.Columns.Add("TableName")

        For Each col As DataColumn In Values.Columns
            Dim row As DataRow = dt.NewRow()
            dt.Rows.Add(row)
            row.Item(0) = col.ColumnName
            'row.Item(1) = col.Table.TableName
        Next

        Me.DataGridView1.DataSource = dt
    End Sub

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
		If _dbc.OutputFolder.Length = 0 Then
			WinUtil.DispMessageBox("Setting OutputFolder Property", WinUtil.MessageBoxType.CriticalOK, String.Empty)
			Me.PropertyGrid1.Focus()
			Exit Sub
		End If
        If Me.txtClassName.Text.Length = 0 Then
            WinUtil.DispMessageBox("Input Class Name", WinUtil.MessageBoxType.CriticalOK, String.Empty)
            Me.txtClassName.Focus()
            Exit Sub
        End If
        If Me.chkDifinition.Checked Then
            If Me.txtTableName.Text.Length = 0 Then
                WinUtil.DispMessageBox("Input TableName Name", WinUtil.MessageBoxType.CriticalOK, String.Empty)
                Me.txtTableName.Focus()
                Exit Sub
            End If
        End If

        Dim dt As DataTable

        dt = Me.DataGridView1.DataSource

        Dim gen As EntityCodeGenerator
        gen = New EntityCodeGenerator
		gen.Namespace = _dbc.TargetNamespace
        gen.ClassName = Me.txtClassName.Text
        If Me.chkDifinition.Checked Then
            gen.TableName = Me.txtTableName.Text
            gen.Generate(Values.Columns, dt.Rows)
        Else
            gen.Generate(Values.Columns)
        End If

        Dim filename As String
		filename = gen.Write(_dbc.OutputFolder)
        exec(filename)
    End Sub

    Private Sub exec(ByVal filename As String)
        Dim exeInfo As ProcessStartInfo
        Dim process As Process

        Try
            exeInfo = New ProcessStartInfo()
            exeInfo.FileName = filename
            'exeInfo.FileName = "notepad.exe"
            'exeInfo.Arguments = filename

            process = System.Diagnostics.Process.Start(exeInfo)

            If process.HasExited Then
                Exit Sub
            End If
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

End Class
